package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.ApplyDiaodongActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.EditBean;
import com.eqdd.yiqidian.bean.StaffBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.ui.base.BaseMultiEditActivity;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;

public class ApplyDiaodongActivity extends BaseMultiEditActivity {
    private ApplyDiaodongActivityCustom dataBinding;
    private static final int NAME = 1;
    private static final int OLD_BUMEN = 2;
    private static final int OLD_GANGWEI = 3;
    private static final int OLD_XINZI = 4;
    private static final int OLD_WENJIAN = 5;
    private static final int OLD_SHITI = 6;
    private static final int NEW_GANGWEI = 8;
    private static final int NEW_XINZI = 9;
    private static final int NEW_WENJIAN = 10;
    private static final int NEW_SHITI = 11;
    private static final int TIME = 12;
    private static final int RESASON = 13;
    private String[] titles = new String[]{
            "原岗位信息",
            "姓名",
            "原任部门",
            "岗位",
            "薪资等级",
            "文件移交内容",
            "物品实物移交内容",
            "调动岗位信息",
            "调动岗位",
            "薪资等级",
            "文件移交内容",
            "物品实体移交内容",
            "调动生效日期",
            "调动原因"
    };
    private String[] hintTexts = new String[]{
            "请输入",
            "请输入",
            "请输入",
            "请输入",
            "请输入",
            "请输入",
            "请输入",
            "请输入",
            "请选择",
            "请选择",
            "请输入",
            "请输入",
            "请选择",
            "请输入"
    };
    private int[] types = new int[]{
            4, 4, 4, 4, 4, 4, 4, 0, 2, 2,
            4, 4, 2, 0
    };

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_apply_diaodong);
        initTopTitleBar(View.VISIBLE, "岗位调动申请");
    }

    @Override
    public void initData() {
        initTopRightText("提交", v -> {
        });
        new HttpPresneter.Builder(HttpConfig.SELECT_STAFF, String.class, this)
                .Params("uid", user.getUname())
                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Object o) {
                        BaseBean<StaffBean> baseBean = GsonUtils.changeGsonToBaseBean((String) o, StaffBean.class);
                        if (baseBean.getStatus() == 200) {
                            if (baseBean.getItems() != null && baseBean.getItems().size() > 0 && !baseBean.getItems().get(0).getCid().equals("0")) {
                                if (user.getAuthen() == 1) {
                                    StaffBean staffBean = baseBean.getItems().get(0);
                                    for (int i = 0; i < titles.length; i++) {
                                        EditBean editBean = new EditBean();
                                        editBean.setTitle(titles[i]);
                                        editBean.setType(types[i]);
                                        editBean.setContent("");
                                        editBean.setTextTitle(titles[i]);
                                        editBean.setHint(hintTexts[i]);
                                        switch (i) {
                                            case 0:
                                            case 7:
                                                editBean.setTopTitle(titles[i]);
                                                editBean.setType(EditBean.TITLE);
                                                break;
                                            case NAME:
                                                editBean.setContent(user.getName());
                                                editBean.setEnter(false);
                                                break;
                                            case OLD_BUMEN:
                                                editBean.setContent(staffBean.getPostname());
                                                editBean.setEnter(false);
                                                break;
                                            case OLD_GANGWEI:
                                                editBean.setContent(staffBean.getCareername());
                                                editBean.setEnter(false);
                                                break;
                                            case OLD_XINZI:
                                                editBean.setContent("薪资等级");
                                                editBean.setEnter(false);
                                                break;
                                        }
                                        editBeans.add(editBean);
                                    }
                                    if (quickAdapter != null) {
                                        ArrayList<EditBean> temp = new ArrayList<>();
                                        temp.addAll(editBeans);
                                        quickAdapter.notifyDataChanged(temp, true);
                                    }

                                } else {
                                    ToastUtil.showShort("请先进行实名认证");
                                    finish();
                                }
                            } else {
                                ToastUtil.showShort("你还没有入住公司呢");
                                finish();
                            }
                        } else {
                            finish();
                        }
                    }
                })
                .build()
                .post();
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (p) {
            case OLD_WENJIAN:
            case OLD_SHITI:
            case NEW_WENJIAN:
            case NEW_SHITI:
                startActivityForResult(AddMultiContentActivity.class, null, p);
                break;

        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Config.INTENT_CONTENT){
            quickAdapter.getItem(requestCode).setContent(data.getStringExtra("content"));
            quickAdapter.notifyItemChanged(requestCode);
        }
    }
}
